﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DelonixRegiaHotel.Classes;
using DelonixRegiaHotel.DBManager;

namespace DelonixRegiaHotel
{
    public partial class RoomBookingAvaliability : Form
    {
        CreateBooking passInfoCreateBooking;
        UpdateBooking passInfoUpdateBooking;
        string tempStoreDays = "";
        DateTime dttempcheckin;
        DateTime dttempcheckout;
        ArrayList getRoomInfo = new ArrayList();
        public RoomBookingAvaliability(string checkSmoking, DateTime checkIn, DateTime checkOut, string days, CreateBooking passCreateBooking, UpdateBooking passUpdateBooking) 
        {
            InitializeComponent();
            passInfoCreateBooking = passCreateBooking;
            passInfoUpdateBooking = passUpdateBooking;
            tempStoreDays = days;
            dttempcheckin = checkIn;
            dttempcheckout = checkOut;
            lblshowsmokingrm.Text = checkSmoking;
            lblshowcheckindate.Text = DateTime.Parse(checkIn.ToString()).ToString("yyyy-MM-dd");
            lblshowcheckoutdate.Text = DateTime.Parse(checkOut.ToString()).ToString("yyyy-MM-dd");
            getRoomInfo = AdvBookingDBManager.GetRoomsByDateAndSmoke(dttempcheckin, dttempcheckout);
             
            String recordSmoking = "";
            if (checkSmoking.Equals("Yes"))
            {
                recordSmoking = checkSmoking;
            }
            else
            {
                recordSmoking = checkSmoking;
            }

            ArrayList documents = new ArrayList();
            ArrayList documents1 = new ArrayList();
            Room tempRm = new Room();
            foreach(var v in getRoomInfo)
            {
                if(v is Room && recordSmoking.Equals("Yes")) // check is it room type
                {
                    tempRm = (Room)v;
                    if (tempRm.SmkRoom.Equals("Yes")) //check whether the specific room is smoking room?
                    {
                        documents.Add(new ListViewItem(tempRm.RoomNo, 0));
                    }
                }

                else if (v is Room && recordSmoking.Equals("No")) //// check is it room type
                {
                    tempRm = (Room)v;
                    if (tempRm.SmkRoom.Equals("No")) ////check whether the specific room is smoking room?
                    {
                        documents1.Add(new ListViewItem(tempRm.RoomNo, 1));
                    }
                }    
            }

            if(checkSmoking.Equals("Yes"))
            {
                ArrayList documents2 = new ArrayList();

                TreeNode documentNode = new TreeNode("Smoking");
                documentNode.Tag = documents2;

                TreeNode firstNode = new TreeNode("Available Room");
                firstNode.Tag = documents;
                documentNode.Nodes.Add(firstNode);
                treroominfo.Nodes.Add(documentNode);
                treroominfo.ImageList = imageList1;
                treroominfo.ImageIndex = 2;
                treroominfo.SelectedImageIndex = 2;
            }

            if (checkSmoking.Equals("No"))
            {
                ArrayList documents2 = new ArrayList();

                TreeNode documentNode1 = new TreeNode("Non - Smoking");
                documentNode1.Tag = documents2;

                TreeNode firstNode = new TreeNode("Available Room");
                firstNode.Tag = documents1;
                documentNode1.Nodes.Add(firstNode);
                treroominfo.Nodes.Add(documentNode1);
                treroominfo.ImageList = imageList1;
                treroominfo.ImageIndex = 2;
                treroominfo.SelectedImageIndex = 2;
            }
  
            lvwroominfo.SmallImageList = imageList1;
            lvwroominfo.View = View.SmallIcon;

            lvwroominfo.MouseClick += new MouseEventHandler(lvwroominfo_MouseClick);
            
        }

        public void lvwroominfo_MouseClick(object sender, MouseEventArgs e)
        {
            ListViewItem click = lvwroominfo.GetItemAt(e.X, e.Y);
            if (click != null)
            {
                StaticSession.TempArrayList = AdvBookingDBManager.GetRoomInfoByIDRmNo(lvwroominfo.SelectedItems[0].Text);
                Room tempRm = new Room();
                RoomType tempRmType = new RoomType();
                foreach(var v in StaticSession.TempArrayList)
                {
                    if(v is Room)
                    {
                        tempRm = (Room)v;
                    }
                    if (v is RoomType)
                    {
                        tempRmType = (RoomType)v;
                    }
                }

                lblshowroomno.Text = tempRm.RoomNo;
                lblshowroomprice.Text = Convert.ToString(tempRmType.RoomPrice);
                lblshowroomtypename.Text = tempRmType.RoomTypeName;
                tbxshowroomdetails.Text = tempRmType.RoomDetails;
            }

        }

        private void treroominfo_AfterSelect(object sender, TreeViewEventArgs e)
        {
            lvwroominfo.Items.Clear();

            ArrayList documents = (ArrayList)e.Node.Tag;
            foreach (ListViewItem lvi in documents)
            {
                lvwroominfo.Items.Add(lvi);
            }
        }

        private void treroominfo_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
        {
            e.Node.ImageIndex = 2;
            e.Node.SelectedImageIndex = 2;
        }

        private void treroominfo_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            e.Node.ImageIndex = 2;
            e.Node.SelectedImageIndex = 2;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            Room tempRm = new Room();
            RoomType tempRmType = new RoomType();
            if (lvwroominfo.SelectedItems.Count > 0)
            {
                foreach(var v in StaticSession.TempArrayList)
                {
                    if(v is Room)   
                    {
                        tempRm = (Room)v;
                    }

                    else if(v is RoomType)
                    {
                        tempRmType = (RoomType)v;
                    }
                }

                //do a for each to check when form is open either create or update
                FormCollection fc = Application.OpenForms;
                foreach (Form frm in fc)
                {
                    if (frm.Text.Equals("CreateBooking"))
                    {
                        passInfoCreateBooking.lblshowrmtype.Text = tempRmType.RoomTypeName;
                        passInfoCreateBooking.lblshowrmno.Text = tempRm.RoomNo;
                        passInfoCreateBooking.tbxrmdetails.Text = tempRmType.RoomDetails;
                        passInfoCreateBooking.lblshowrmprice.Text = Convert.ToString(tempRmType.RoomPrice);
                        double countTotalPrice = Convert.ToDouble(tempStoreDays) * Convert.ToDouble(passInfoCreateBooking.lblshowrmprice.Text);
                        passInfoCreateBooking.lblshowamount.Text = Convert.ToString(countTotalPrice);
                        passInfoCreateBooking.lblshowbalance.Text = "-";
                        this.Hide();
                    }

                    if(frm.Text.Equals("UpdateBooking"))
                    {
                        passInfoUpdateBooking.lblshowrmtype.Text = tempRmType.RoomTypeName;
                        passInfoUpdateBooking.lblshowrmno.Text = tempRm.RoomNo;
                        passInfoUpdateBooking.tbxrmdetails.Text = tempRmType.RoomDetails;
                        passInfoUpdateBooking.lblshowrmprice.Text = Convert.ToString(tempRmType.RoomPrice);
                        double countTotalPrice = Convert.ToDouble(tempStoreDays) * Convert.ToDouble(passInfoUpdateBooking.lblshowrmprice.Text);
                        passInfoUpdateBooking.lblshowamount.Text = Convert.ToString(countTotalPrice);
                        passInfoUpdateBooking.lblshowbalance.Text = "-";
                        this.Hide();
                    }
                }


            }
        }












        }
    }

